package cn.tannn.surenessdemo.user.dao;

import cn.tannn.surenessdemo.user.entity.AuthRoute;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 前端路由
 * @author tan
 * @date 2022-07-18 11:00:57
 */
public interface AuthRouteDao extends JpaRepository<AuthRoute, Long> {

    /**
     * 查询 路由
     * @param routeCode AuthRoute.code
     * @return AuthRoute
     */
    AuthRoute findByCode(String routeCode);

    /**
     * 删除路由
     * @param routeCode AuthRoute.code
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    void deleteByCode(String routeCode);

    /**
     *  删除路由
     * @param routeCodes AuthRoute.code
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    void deleteByCodeIn(List<String> routeCodes);


    /**
     * 判断路由是否存在
     * @param code 路由code
     * @return boolean
     */
    boolean existsByCode(String code);


    /**
     * 查询路由
     * @param status 节点状态（0、禁用；1、启用）默认启用
     * @return
     */
    List<AuthRoute> findByStatus(String status);





    /**
     * 查询用户路由
     * @param userCode 用户code
     * @return list of AuthRoute
     */
    @Query("select aroute from AuthUser au left join AuthUserRoleBind  aur on au.code=aur.userCode " +
            "left join AuthRole  arole on aur.roleCode = arole.code " +
            "left join AuthRoleResourceBind arr on aur.roleCode=arr.roleCode " +
            "left join AuthRouteResourceBind  arouter on arr.resourceCode=arouter.resourceCode " +
            "left join AuthRoute aroute on arouter.routeCode = aroute.code " +
            "where au.code = :userCode and arole.status=1 and aroute.status= 1 ")
    List<AuthRoute> findByUser(@Param("userCode") String userCode);

}
